<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    这样的代码才叫 Pythonic.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-develop/这样的代码才叫 Pythonic"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  这样的代码才叫 Pythonic.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/develop/%E8%BF%99%E6%A0%B7%E7%9A%84%E4%BB%A3%E7%A0%81%E6%89%8D%E5%8F%AB%20Pythonic/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/develop/">develop</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">9 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="这样的代码才叫-Pythonic"><a href="#这样的代码才叫-Pythonic" class="headerlink" title="这样的代码才叫 Pythonic"></a>这样的代码才叫 Pythonic</h1><p>Python由于语言的简洁性，让我们以人类思考的方式来写代码，新手更容易上手，老鸟更爱不释手。</p>
<p>要写出 Pythonic（优雅的、地道的、整洁的）代码，还要平时多观察那些大牛代码，这里明哥收集了一些比较常见的 Pythonic 写法，帮助你养成写优秀代码的习惯。</p>
<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHklazcTAqHwNiakxwcrlNUFmx9RjNGxR0ghGKMoO9ubibCib9CHYkkT2HA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<h2 id="变量交换"><a href="#变量交换" class="headerlink" title="变量交换"></a><strong>变量交换</strong></h2><p>交换两个变量的值，正常都会想利用一个中间临时变量来过渡。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;tmp &#x3D; a&#96;&#96;a &#x3D; b&#96;&#96;b &#x3D; tmp&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>能用一行代码解决的（并且不影响可读性的），决不用三行代码。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;a,b &#x3D; b,a&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwH79IdgwBuCldKPGDicPyLfzel3DBo8Odgd8VbbYiaUZG3qBz5DRhEbmgg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>列表推导</strong>  </p>
<hr>
<p>下面是一个非常简单的 for 循环。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;my_list &#x3D; []&#96;&#96;for i in range(10):&#96; &#96;my_list.append(i*2)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>在一个 for 循环中，如果逻辑比较简单，不如试用一下列表的列表推导式，虽然只有一行代码，但也逻辑清晰。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;my_list &#x3D; [i*2 for i in range(10)]&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHgvU6jQlC6U6g2F1sBAUicFbcmNibDa0d3vHpawVWfn5654o7uLI7icozw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>单行表达式</strong>  </p>
<hr>
<p>上面两个案例，都将多行代码用另一种方式写成了一行代码。</p>
<p>这并不意味着，代码行数越少，就越 Pythonic 。</p>
<p>比如下面这样写，就不推荐。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;print(&#39;hello&#39;); print(&#39;world&#39;)&#96;&#96;if x &#x3D;&#x3D; 1: print(&#39;hello,world&#39;)&#96;&#96;if &lt;complex comparison&gt; and &lt;other complex comparison&gt;:&#96; &#96;# do something&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>建议还是按照如下的写法来</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;print（&#39;hello&#39;）&#96;&#96;print（&#39;world&#39;）&#96;&#96;if x &#x3D;&#x3D; 1:&#96; &#96;print(&#39;hello,world&#39;)&#96;&#96;cond1 &#x3D; &lt;complex comparison&gt;&#96;&#96;cond2 &#x3D; &lt;other complex comparison&gt;&#96;&#96;if cond1 and cond2:&#96; &#96;# do something&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHw0gbr2nQhORCNib5Wlbuo2ane4qziancmoHuyPK5NsUThZXXnFbiaLdlA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>带索引遍历</strong>  </p>
<hr>
<p>使用 for 循环时，如何取得对应的索引，初学者习惯使用 range + len </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;for i in range(len(my_list)):&#96; &#96;print(i, &quot;--&gt;&quot;, my_list[i])&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>更好的做法是利用 enumerate 这个内置函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;for i,item in enumerate(my_list):&#96; &#96;print(i, &quot;--&gt;&quot;,item)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwH3Njic1JR7J9liafR0Y2TeXIwWZaJPOumK6TLrVGt0Vue5ibQU14iaghCBA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>序列解包</strong>  </p>
<hr>
<p>使用 * 可以对一个列表解包</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;a, *rest &#x3D; [1, 2, 3]&#96;&#96;# a &#x3D; 1, rest &#x3D; [2, 3]&#96;&#96;a, *middle, c &#x3D; [1, 2, 3, 4]&#96;&#96;# a &#x3D; 1, middle &#x3D; [2, 3], c &#x3D; 4&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHyV2C0tibEjhEial4SrXA0YZXaZia8UFjhXbCGgUcn3Yja2kl8ByTmM38w/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>字符串拼接</strong>  </p>
<hr>
<p>如果一个列表（或者可迭代对象）中的所有元素都是字符串对象，想要将他们连接起来，通常做法是</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;letters &#x3D; [&#39;s&#39;, &#39;p&#39;, &#39;a&#39;, &#39;m&#39;]&#96;&#96;s&#x3D;&quot;&quot;&#96;&#96;for let in letters:&#96; &#96;s +&#x3D; let&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>更推荐的做法是使用 join 函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;letters &#x3D; [&#39;s&#39;, &#39;p&#39;, &#39;a&#39;, &#39;m&#39;]&#96;&#96;word &#x3D; &#39;&#39;.join(letters)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHCibcNLkqr1tK72ibibS0kvbO9NbItSYoENiam42pAPz5eAebt3AU8ricdfQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>真假判断</strong>  </p>
<hr>
<p>判断一个变量是否为真（假），新手习惯直接使用 == 与 True、False、None 进行对比</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;if attr &#x3D;&#x3D; True:&#96; &#96;print(&#39;True!&#39;)&#96;&#96;if attr &#x3D;&#x3D; None:&#96; &#96;print(&#39;attr is None!&#39;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>实际上，**””、[]、{}** 这些没有任何元素的容器都是假值，可直接使用 <strong>if not xx</strong> 来判断。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;if attr:&#96; &#96;print(&#39;attr is truthy!&#39;)&#96;&#96;if not attr:&#96; &#96;print(&#39;attr is falsey!&#39;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwH9gMe7RFicNoE1XfuIANfyphuYhTLAjZ5OW2gwU2NqySzBlMxEX5hPxA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>访问字典元素</strong>  </p>
<hr>
<p>当直接使用 <strong>[]</strong> 来访问字典里的元素时，若key不存在，是会抛异常的，所以新会可能会先判断一下是否有这个 key，有再取</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;d &#x3D; &#123;&#39;hello&#39;: &#39;world&#39;&#125;&#96;&#96;if d.has_key(&#39;hello&#39;):&#96; &#96;print(d[&#39;hello&#39;])    # prints &#39;world&#39;&#96;&#96;else:&#96; &#96;print(&#39;default_value&#39;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>更推荐的做法是使用 <strong>get</strong> 来取，如果没有该 key 会默认返回 None（当然你也可以设置默认返回值）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;d &#x3D; &#123;&#39;hello&#39;: &#39;world&#39;&#125;&#96;&#96;print(d.get(&#39;hello&#39;, &#39;default_value&#39;)) # prints &#39;world&#39;&#96;&#96;print(d.get(&#39;thingy&#39;, &#39;default_value&#39;)) # prints &#39;default_value&#39;&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwH5Z3BhtxFYWl1gjYae3G8pdEXRYTbP5xf4r9ULlva0BicoPHRyCvYDnQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>操作列表</strong>  </p>
<hr>
<p>下面这段代码，会根据条件过滤过列表中的元素</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;a &#x3D; [3, 4, 5]&#96;&#96;b &#x3D; []&#96;&#96;for i in a:&#96; &#96;if i &gt; 4:&#96; &#96;b.append(i)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>实际上可以使用列表推导或者高阶函数 filter 来实现</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;a &#x3D; [3, 4, 5]&#96;&#96;b &#x3D; [i for i in a if i &gt; 4]&#96;&#96;# Or:&#96;&#96;b &#x3D; filter(lambda x: x &gt; 4, a)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>除了 filter 之外，还有 map、reduce 这两个函数也很好用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;a &#x3D; [3, 4, 5]&#96;&#96;b &#x3D; map(lambda i: i + 3, a)&#96;&#96;# b: [6,7,8]&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHJQ6HeBxl8dibSmCKpWZ2uzInicfCsNh4odicLa1kZibQicVvORicZWWl4KfA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>文件读取</strong>  </p>
<hr>
<p>文件读取是非常常用的操作，在使用完句柄后，是需要手动调用 close 函数来关闭句柄的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;fp &#x3D; open(&#39;file.txt&#39;)&#96;&#96;print(fp.read())&#96;&#96;fp.close()&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果代码写得太长，即使你知道需要手动关闭句柄，却也会经常会漏掉。因此推荐养成习惯使用 <strong>with open</strong> 来读写文件，上下文管理器会自动执行关闭句柄的操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;with open(&#39;file.txt&#39;) as fp:&#96; &#96;for line in fp.readlines():&#96; &#96;print(line)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHicyS5CI0JQ512B81UEIfGcibAQJ8E2IdosuZIy5ezKsEswbwmt60xOww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>代码续行</strong>  </p>
<hr>
<p>将一个长度较长的字符串放在一行中，是很影响代码可读性的(下面代码可向左滑动)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;long_string &#x3D; &#39;For a long time I used to go to bed early. Sometimes, when I had put out my candle, my eyes would close so quickly that I had not even time to say “I’m going to sleep.”&#39;&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>稍等注重代码可读性的人，会使用三个引号 <strong>\</strong>来续写</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;long_string &#x3D; &#39;For a long time I used to go to bed early. &#39; \&#96; &#96;&#39;Sometimes, when I had put out my candle, &#39; \&#96; &#96;&#39;my eyes would close so quickly that I had not even time to say “I’m going to sleep.”&#39;&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>不过，对我来说，我更喜欢这样子写 使用括号包裹 <strong>()</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;long_string &#x3D; (&#96; &#96;&quot;For a long time I used to go to bed early. Sometimes, &quot;&#96; &#96;&quot;when I had put out my candle, my eyes would close so quickly &quot;&#96; &#96;&quot;that I had not even time to say “I’m going to sleep.”&quot;&#96;&#96;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>导包的时候亦是如此</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;from some.deep.module.inside.a.module import (&#96; &#96;a_nice_function, another_nice_function, yet_another_nice_function)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHjr6ib80KaLEfiaWCUrW5CRvFtOavAU4DXSTuzeyJqnOE6ibSkG1AWJpZg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>显式代码</strong>  </p>
<hr>
<p>有时候出于需要，我们会使用一些特殊的魔法来使代码适应更多的场景不确定性。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;def make_complex(*args):&#96; &#96;x, y &#x3D; args&#96; &#96;return dict(**locals())&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>但若非必要，请不要那么做。无端增加代码的不确定性，会让原先本就动态的语言写出更加动态的代码。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;def make_complex(x, y):&#96; &#96;return &#123;&#39;x&#39;: x, &#39;y&#39;: y&#125;&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwH01WELfAMUxxQ4NkFJiaHw6JWWNkDCASzsIcMxSbt6koC6qu2xx3boNw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>使用占位符</strong>  </p>
<hr>
<p>对于暂不需要，却又不得不接收的的变量，请使用占位符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;filename &#x3D; &#39;foobar.txt&#39;&#96;&#96;basename, _, ext &#x3D; filename.rpartition(&#39;.&#39;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHfLciaCqaEYJwHeibJjDoCfAdib5RZ8ib3tabKapO4G0ZRUuhXeGe9ibXbRA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>链式比较</strong>  </p>
<hr>
<p>对于下面这种写法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;score &#x3D; 85&#96;&#96;if score &gt; 80 and score &lt; 90:&#96; &#96;print(&quot;良好&quot;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>其实还有更好的写法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;score &#x3D; 85&#96;&#96;if 80 &lt; score &lt; 90:&#96; &#96;print(&quot;良好&quot;)&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果你理解了上面的链式比较操作，那么你应该知道为什么下面这行代码输出的结果是 False</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;&gt;&gt;&gt; False &#x3D;&#x3D; False &#x3D;&#x3D; True&#96; &#96;False&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><img src="https://mmbiz.qpic.cn/mmbiz_png/Pn4Sm0RsAugo5SicrbBWhBdrvsmIrWNwHQ0nQgF9YLYkvAdvw4LkjAovg7MW664ZejVJtBa4hY7167m9shlaJSQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></strong></p>
<p><strong>三目运算</strong>  </p>
<hr>
<p>对于简单的判断并赋值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;age &#x3D; 20&#96;&#96;if age &gt; 18:&#96; &#96;type &#x3D; &quot;adult&quot;&#96;&#96;else:&#96; &#96;type &#x3D; &quot;teenager&quot;&#96;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>其实是可以使用三目运算，一行搞定。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">age &#x3D; 20  b &#x3D; &quot;adult&quot; if age &gt; 18 else &quot;teenager&quot;</span><br></pre></td></tr></table></figure>



<p><img src="https://mmbiz.qpic.cn/mmbiz_png/1hReHaqafafJYoFH7OAhmUdTjo35vvJTy1lRVjG2CzFP3arfVRDqI7a8PSS6Sx5LialaFTE1HFu2N4OIhL8jP9g/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/Pn4Sm0RsAuge5LksSBIebanurzZbauYRUiciaSLZDju7lfe7HrEEmaTZFz52ASkiaK35qv36IpPo9IbTBtkWO4MJg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">**更多精彩推荐**</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650788258&idx=1&sn=8c8120bba90e3f1d9a3ad7411f33ace0&chksm=bea7a07189d02967fc29cac18a8074e6b778417338693aa7fd030333c42ba987316af537b4b0&scene=21#wechat_redirect">☞</a><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650788258&idx=1&sn=8c8120bba90e3f1d9a3ad7411f33ace0&chksm=bea7a07189d02967fc29cac18a8074e6b778417338693aa7fd030333c42ba987316af537b4b0&scene=21#wechat_redirect">机器人也开始”怕疼”了？科学家开发无需人工干预即可”自愈”的机器人  
</a></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650788258&idx=1&sn=8c8120bba90e3f1d9a3ad7411f33ace0&chksm=bea7a07189d02967fc29cac18a8074e6b778417338693aa7fd030333c42ba987316af537b4b0&scene=21#wechat_redirect">☞</a><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650791237&idx=1&sn=000a920dd1d7c92b015137ecf0eb981b&chksm=bea7ac9689d02580c2bc20812405ec5ed2e7414837370f4a3d1070ad58f21a2a505c87db714e&scene=21#wechat_redirect">对话指令集创始人兼CEO潘爱民：面向未来的新型物联网操作系统 | 人物志</a></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650754382&idx=3&sn=7774fe80d79901abfa01500971230e84&chksm=bea73c9d89d0b58bbca823368137198834d3c4300f00d11917ac93a6dbaabb8378813c66a0a5&scene=21#wechat_redirect">☞</a><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzA5MzY4NTQwMA==&mid=2651033873&idx=1&sn=a72783457162a649a60c4270e94eb491&chksm=8bad22e6bcdaabf01e7aaba171934c4a70d7e820504aa695ca6b8f9ee95a45a85be93a91e535&scene=21#wechat_redirect">@程序员，什么才是“2020-1024”的正确打开姿势？</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650750309&idx=2&sn=e6772d93425a7a23a9d53c793274b9bd&chksm=bea74cb689d0c5a0a5958c2f20aaa0fdf3616a3089b6e61082e47f6639ed593951c7d39e5698&scene=21#wechat_redirect">☞</a><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzI0ODcxODk5OA==&mid=2247529015&idx=1&sn=5713c9afae9c8be69fe0a657f7746d76&chksm=e99e41cedee9c8d8a518fa664adb4a4475ba6e32db882805093d91c2a94c0c4b6fb17cece178&scene=21#wechat_redirect">最新！百度首发 OCR 自训练平台 EasyDL OCR</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650746260&idx=1&sn=ec250a1510b9cf57c8ba19578decefb1&chksm=bea75c4789d0d551e79436a31b723defb9f792c09c0d78c83d04b011a5c1758175aad851c534&scene=21#wechat_redirect">☞</a><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzA3MjY1MTQwNQ==&mid=2649844744&idx=1&sn=8f4144a8e0720a61f6f87eb80ee00335&chksm=871ecbeab06942fc62da9c230d221a13eb74ff9eaeb4a32a9cc2d9af29e847b1c8b718441dd4&scene=21#wechat_redirect">SQL分页查询方案的性能对比</a></p>
<p>☞<a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2MTE1NDk2Mg==&mid=2247507375&idx=1&sn=db44a14e7e1497987501df7557820e65&chksm=fc7f8352cb080a4489f7f110f5e28fcd2948b4fa870fdfc462374422aaf04d91706e440f97db&scene=21#wechat_redirect">2021年，很可能是以太坊的“高光之年”</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;mmbiz.qpic.cn&#x2F;mmbiz_png&#x2F;Pn4Sm0RsAugOHoUicBekr3guXbjm0KkicU7iaibnuR7Z4qPYHp4LUBHicYeBx3p5H5cKyCibSSapo9b0RjPficFdgOqpg&#x2F;640?wx_fmt&#x3D;png&amp;tp&#x3D;webp&amp;wxfrom&#x3D;5&amp;wx_lazy&#x3D;1&amp;wx_co&#x3D;1)</span><br><span class="line"></span><br><span class="line">点分享</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;mmbiz.qpic.cn&#x2F;mmbiz_png&#x2F;Pn4Sm0RsAugOHoUicBekr3guXbjm0KkicU4YvCLQDSXxzNJAgPia8JwX0micmkOrs4G1wmf3LOcnNl5nYrqEZud7uQ&#x2F;640?wx_fmt&#x3D;png&amp;tp&#x3D;webp&amp;wxfrom&#x3D;5&amp;wx_lazy&#x3D;1&amp;wx_co&#x3D;1)</span><br><span class="line"></span><br><span class="line">点点赞</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;mmbiz.qpic.cn&#x2F;mmbiz_png&#x2F;Pn4Sm0RsAugOHoUicBekr3guXbjm0KkicUuEaCBibBecgM1vBvth2SicVCYrPVd6d8x2H3lALg3fZnO9du4c91OwKQ&#x2F;640?wx_fmt&#x3D;png&amp;tp&#x3D;webp&amp;wxfrom&#x3D;5&amp;wx_lazy&#x3D;1&amp;wx_co&#x3D;1)</span><br><span class="line"></span><br><span class="line">点在看</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/develop/%E8%BF%99%E6%A0%B7%E7%9A%84%E4%BB%A3%E7%A0%81%E6%89%8D%E5%8F%AB%20Pythonic/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/develop/" rel="tag">develop</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/develop/%E7%9B%B8%E8%A7%81%E6%81%A8%E6%99%9A%EF%BC%81Python%E9%82%A3%E4%BA%9B%E4%B8%8D%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84%E5%A4%A7%E5%9D%91/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            相见恨晚！Python那些不为人知的大坑.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/django/10-RESTful%E6%9E%B6%E6%9E%84%E5%92%8CDRF%E8%BF%9B%E9%98%B6/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">10-RESTful架构和DRF进阶.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>